!     ******************************************************************
!     *                                                                *
!     *   MESH GENERATION MODULE                                       *
!     *                                                                *
!     ******************************************************************

!     ******************************************************************
!     *                                                                *
!     *   COPYRIGHT (C) ANTONY JAMESON 1988                            *
!     *                                                                *
!     ******************************************************************

!     NAME=GMESH
      SUBROUTINE GMESH

!     ******************************************************************
!     *                                                                *
!     *   GENERATES THE MESH                                           *
!     *                                                                *
!     ******************************************************************
      USE CARBUNCLE
      IMPLICIT NONE
!     ******************************************************************
!     *                                                                *
!     *   DIMENSION CONTROL PARAMETERS                                 *
!     *                                                                *
!     ******************************************************************

!      PARAMETER  (IDM=385,JDM=65)         ! MOVED TO MODULE CARBUNCLE
!      PARAMETER  (IDN=IDM+1,JDN=JDM+1)
!      PARAMETER  (IMX=385)

!     ******************************************************************

!      COMMON/LIM/ NX,NY,IL,JL,IE,JE,ITL,ITU ! MOVED TO MODULE CARBUNCLE
!      COMMON/GRD/ X(IDM,JDM,2),VOL(IDN,JDN)
!      COMMON/SYM/ ISYM
!      COMMON/CRD/ A0(IDM),S0(IDM),B0(JDM),XSING,YSING,TRAIL,SLOPT,
!     .            BOUNDX,BOUNDY,BX,XTE,XLIM,AX,AY,SY
!      COMMON/GEO/ XP(IMX),YP(IMX),NP
!      COMMON/OUT/ CP(IDM),SCAL,CHORD,XM,YM,CL,CD,CM
!      COMMON/IMS/ IMESH,IIN

!     ******************************************************************
      INTEGER :: IWRIT,IMAX,JMAX,I,J,L,ILE

      IWRIT     = 6
      IL        = NX  +1
      JL        = NY  +1
      IE        = NX  +2
      JE        = NY  +2

!     IF IMESH.EQ.1 READ THE MESH

      IF (IMESH.EQ.1) THEN          ! READ THE MESH FROM MESH.D
         OPEN (IIN,FILE='MESH.D',ACCESS='SEQUENTIAL',FORM='UNFORMATTED')
         READ (IIN) IMAX,JMAX,((X(I,J,1),X(I,J,2),I=1,IMAX),J=1,JMAX)
         SCAL = 1.
         RETURN
      END IF

!     IF ISYM.LT.0 GENERATE POLAR COORDINATES FOR A CIRCLULAR CYLINDER

      IF (ISYM.LT.0) THEN
         CALL CIRCLE
         CHORD = 2.
         XM    = -.5
         YM    = 0.
         XTE   = 1.
         ITL   = 1
         ITU   = IL
         RETURN
      END IF

      IF (ISYM.GE.0) THEN
         CALL COORD  ! DEFINE THE TRANSFORMATION FOR GENERATING THE MESH
         L   = .5000005*XTE*(IL  -1)
         ILE = IL/2  +1
         ITL = ILE  -L
         ITU = ILE  +L
         CALL GEOM

!        GENERATE THE MESH

         CALL MESH

!        PRINT THE MESH PARAMETERS

   21    WRITE (IWRIT,600)
         WRITE (IWRIT,22)
   22    FORMAT(42H0MAPPED COORDINATES AND FAR FIELD FUNCTION/&
                1H0,2X,12H     A0     ,12H     S0     )
         DO 24 I=1,IL
   24    WRITE (IWRIT,610) A0(I),S0(I)
         WRITE (IWRIT,26)
   26    FORMAT(1H0,2X,12H   BOUNDX   ,12H     BX     ,12H     AX     ,&
                12H     XTE    )
         WRITE (IWRIT,610) BOUNDX,BX,AX,XTE
         WRITE (IWRIT,600)
         WRITE (IWRIT,32)
   32    FORMAT(1H0,2X,12H     B0     )
         DO 34 J=1,JL
   34    WRITE (IWRIT,610) B0(J)
         WRITE (IWRIT,36)
   36    FORMAT(1H0,2X,12H   BOUNDY   ,12H     AY     ,12H     SY     )
         WRITE (IWRIT,610) BOUNDY,AY,SY
         RETURN
      END IF

!     ******************************************************************

  600 FORMAT(1H1)
  610 FORMAT(1X,6F12.4)
      END SUBROUTINE GMESH

